app.controller('SetPowerModalCtrl', ['$scope', '$modal', '$log', '$http', '$window', '$localStorage', '$state', '$location',
        function ($scope, $modal, $log, $http, $window, $localStorage, $state, $location) {
            $scope.items = ['item1', 'item2', 'item3'];
            //console.log($state.params);
            var  layer_id, max_power,maxpower,room_id,auto_regain_min;
            var ModalInstanceCtrl = function ($scope, $modalInstance, items) {
                $scope.oo = {};    
                $scope.oo.max_power =  maxpower;         
                $scope.items = items;
                $scope.selected = {
                    item: $scope.items[0]
                };
                $scope.ok = function () {
                    max_power = $scope.oo.max_power;                                      
                    $modalInstance.close($scope.selected.item);
                };

                $scope.cancel = function () {
                    $modalInstance.dismiss('cancel');
                };
            };
            $scope.setpower = function(para,tid,mp,tauto){
                layer_id = para;
                room_id = tid; 
                maxpower = mp;      
                auto_regain_min = tauto;                
                $scope.open();
            }
            $scope.open = function (size) {
                var modalInstance = $modal.open({
                    templateUrl: 'setpowerModalContent.html',
                    controller: ModalInstanceCtrl,
                    size: size,
                    resolve: {
                        items: function () {
                            return $scope.items;
                        }
                    }
                });
                modalInstance.result.then(function (selectedItem) {                  
                    $http.post(
                        $scope.app.backendurl+"tbl-electricity-manage/set-one-params",
                        {
                           
                            'layer_id': layer_id,
                            'room_id':room_id,
                            'max_power': max_power,   
                            'auto_regain_min': auto_regain_min,                         
                            'token':$localStorage.token                   

                        }
                    ).success(function (data) {
                      console.log(data);
                      if(data.code==0){
                        alert(data.msg);
                        $window.location.reload('app/control/selectricdetail');                      

                      }
                      if(data.code==1){
                        alert(data.msg)
                      }
                       if(data.code==20001){
                         $location.path('access.signin');
                       }
                    }).error(function (data) {
                        $scope.authError = data.error.code;
                        if ($scope.authError == 401) {
                            $location.path('access.signin');
                        }
                    })
                });
            };
        }])


app.controller('SetTimeModalCtrl', ['$scope', '$modal', '$log', '$http', '$window', '$localStorage', '$state', '$location',
        function ($scope, $modal, $log, $http, $window, $localStorage, $state, $location) {
            $scope.items = ['item1', 'item2', 'item3'];
            //console.log($state.params);
            var  layer_id,room_id,auto_regain_min,autorm,max_power;
            var ModalInstanceCtrl = function ($scope, $modalInstance, items) {
                $scope.oo = {};
                $scope.oo.auto_regain_min = autorm;
                $scope.items = items;
                $scope.selected = {
                    item: $scope.items[0]
                };
                $scope.ok = function () {                    
                    auto_regain_min = $scope.oo.auto_regain_min;
                                      
                    $modalInstance.close($scope.selected.item);
                };

                $scope.cancel = function () {
                    $modalInstance.dismiss('cancel');
                };
            };
            $scope.settime = function(para,tid,mp,tauto){
                layer_id = para;
                room_id = tid;
                max_power = mp;
                autorm = tauto;
                $scope.open();
            }
            $scope.open = function (size) {
                var modalInstance = $modal.open({
                    templateUrl: 'settimeModalContent.html',
                    controller: ModalInstanceCtrl,
                    size: size,
                    resolve: {
                        items: function () {
                            return $scope.items;
                        }
                    }
                });
                modalInstance.result.then(function (selectedItem) {
                    
                    $http.post(
                        $scope.app.backendurl+"tbl-electricity-manage/set-one-params",
                        {
                           
                            'layer_id': layer_id,
                            'room_id':room_id,
                            'max_power':max_power,
                            'auto_regain_min': auto_regain_min,      
                            'token':$localStorage.token                   

                        }
                    ).success(function (data) {
                     //console.log(data);
                      if(data.code==0){
                        alert(data.msg);
                        $window.location.reload('app/control/selectricdetail');                      

                      }
                      if(data.code==1){
                        alert(data.msg)
                      }
                       if(data.code==20001){
                         $location.path('access.signin');
                       }
                    }).error(function (data) {
                        $scope.authError = data.error.code;
                        if ($scope.authError == 401) {
                            $location.path('access.signin');
                        }
                    })
                });
            };
        }])
